package com.xpxrule.util;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xpxrule.dataobject.bo.FieldInfoBo;
import com.xpxrule.dataobject.bo.RuleConditionItemBo;
import com.xpxrule.dataobject.bo.RuleResultItemBo;
import com.xpxrule.dataobject.dao.*;
import com.xpxrule.dataobject.vo.*;

import java.util.List;

/**
 * 模型转换工具类
 */
public class ModelConvertUtil {

    public static final String MODEL_ITEM_KEY_FORMAT ="%s.%s";
    public static BusinessModel convertBusinessModelVoToBusinessModel(BusinessModelVo vo) {
        BusinessModel dao = new BusinessModel();
        fillBusinessModelFromBusinessModelVo(dao, vo);
        return dao;
    }

    public static BusinessModelVo convertBusinessModelToBusinessModelVo(BusinessModel dao) {
        BusinessModelVo vo = new BusinessModelVo();
        vo.setId(String.valueOf(dao.getId()));
        vo.setModelName(dao.getModelName());
        vo.setModelKey(dao.getModelKey());
        vo.setDescription(dao.getDescription());
        vo.setIsEnable(dao.getIsEnable());
        return vo;
    }

    public static ModelItem convertModelItemVoToModelItem(ModelItemVo vo) {
        ModelItem dao = new ModelItem();
        fillModelItemFromModelItemVo(dao, vo);
        return dao;
    }

    public static void fillBusinessModelFromBusinessModelVo(BusinessModel old, BusinessModelVo vo) {
        old.setModelName(vo.getModelName());
        old.setModelKey(vo.getModelKey());
        old.setDescription(vo.getDescription());
        old.setIsEnable(vo.getIsEnable());
    }

    public static void fillModelItemFromModelItemVo(ModelItem old, ModelItemVo vo) {
        old.setModelKey(vo.getModelKey());
        old.setFieldKey(String.format(MODEL_ITEM_KEY_FORMAT, vo.getModelKey(), vo.getFieldKey()));
        old.setFieldName(vo.getFieldName());
        old.setFieldValueType(vo.getFieldValueType());
        old.setFieldType(vo.getFieldType());
        old.setDescription(vo.getDescription());
        old.setIsEnable(vo.getIsEnable());
    }

    public static ModelItemVo convertModelItemToModelItemVo(ModelItem modelItem) {
        ModelItemVo vo = new ModelItemVo();
        vo.setId(String.valueOf(modelItem.getId()));
        vo.setModelKey(modelItem.getModelKey());
        vo.setFieldKey(modelItem.getFieldKey());
        vo.setFieldName(modelItem.getFieldName());
        vo.setFieldValueType(modelItem.getFieldValueType());
        vo.setFieldType(modelItem.getFieldType());
        vo.setDescription(modelItem.getDescription());
        vo.setIsEnable(modelItem.getIsEnable());
        return vo;
    }

    public static RuleScenarios convertRuleScenariosVoToRuleScenarios(RuleScenariosVo vo) {
        RuleScenarios dao = new RuleScenarios();
        fillRuleScenariosFromRuleScenariosVo(dao, vo);
        return dao;
    }

    public static void fillRuleScenariosFromRuleScenariosVo(RuleScenarios dao, RuleScenariosVo vo) {
        dao.setId(StringUtils.isEmpty(vo.getId()) ? null : Long.valueOf(vo.getId()));
        dao.setScenariosKey(vo.getScenariosKey());
        dao.setScenariosName(vo.getScenariosName());
        dao.setConditionModel(vo.getConditionModelName());
        dao.setResultModel(vo.getResultModelName());
        dao.setDescription(vo.getDescription());
        dao.setIsEnable(vo.getIsEnable());
    }

    public static RuleScenariosVo convertRuleScenariosToRuleScenariosVo(RuleScenarios dao) {
        RuleScenariosVo vo = new RuleScenariosVo();
        vo.setId(String.valueOf(dao.getId()));
        vo.setScenariosKey(dao.getScenariosKey());
        vo.setScenariosName(dao.getScenariosName());
        vo.setConditionModelName(dao.getConditionModel());
        vo.setResultModelName(dao.getResultModel());
        vo.setDescription(dao.getDescription());
        vo.setIsEnable(dao.getIsEnable());
        return vo;
    }

    public static Rule convertRuleVoToRule(RuleVo vo) {
        Rule rule = new Rule();
        fillRuleFromRuleVo(rule, vo);
        return rule;
    }

    public static void fillRuleFromRuleVo(Rule rule, RuleVo vo) {
        ObjectMapper objectMapper = new ObjectMapper();
        rule.setId(vo.getId() == null ? null : Long.valueOf(vo.getId()));
        rule.setRuleName(vo.getRuleName());
        rule.setScenariosKey(vo.getScenariosKey());
        try {
            rule.setConditionDetail(objectMapper.writeValueAsString(vo.getConditionDetail()));
            rule.setResultDetail(objectMapper.writeValueAsString(vo.getResultDetail()));
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        rule.setResultAction(vo.getResultAction());
        rule.setDescription(vo.getDescription());
        rule.setStartEffectTime(vo.getStartEffectTime());
        rule.setEndEffectTime(vo.getEndEffectTime());
        rule.setIsEnable(vo.getIsEnable());
    }

    public static RuleVo convertRuleToRuleVo(Rule rule) {
        RuleVo vo = new RuleVo();
        fillRuleVoFromRule(vo, rule);
        return vo;
    }

    public static void fillRuleVoFromRule(RuleVo vo, Rule rule) {
        vo.setId(String.valueOf(rule.getId()));
        vo.setRuleName(rule.getRuleName());
        vo.setScenariosKey(rule.getScenariosKey());
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            List<RuleConditionItemBo> conditionItemBoList = objectMapper.readValue(rule.getConditionDetail(), new TypeReference<List<RuleConditionItemBo>>() {
            });
            vo.setConditionDetail(conditionItemBoList);
            List<RuleResultItemBo> resultItemBoList = objectMapper.readValue(rule.getResultDetail(), new TypeReference<List<RuleResultItemBo>>() {
            });
            vo.setResultDetail(resultItemBoList);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        vo.setResultAction(rule.getResultAction());
        vo.setDescription(rule.getDescription());
        vo.setStartEffectTime(rule.getStartEffectTime());
        vo.setEndEffectTime(rule.getEndEffectTime());
//        vo.setIsDeleted(rule.getIsDeleted());
        vo.setIsEnable(rule.getIsEnable());
    }

    public static EnumConstant convertEnumConstantVoToEnumConstant(EnumConstantVo vo) {
        EnumConstant dao = new EnumConstant();
        fillEnumConstantFromEnumConstantVo(dao, vo);
        return dao;
    }

    public static void fillEnumConstantFromEnumConstantVo(EnumConstant dao, EnumConstantVo vo) {
        dao.setConstantKey(vo.getConstantKey());
        dao.setConstantName(vo.getConstantName());
        dao.setItemType(vo.getItemType());
        dao.setDescription(vo.getDescription());
        dao.setIsEnable(vo.getIsEnable());
    }

    public static EnumConstantVo convertEnumConstantToEnumConstantVo(EnumConstant enumConstant) {
        EnumConstantVo vo = new EnumConstantVo();
        fillEnumConstantVoFromEnumConstant(vo, enumConstant);
        return vo;
    }

    private static void fillEnumConstantVoFromEnumConstant(EnumConstantVo vo, EnumConstant enumConstant) {
        vo.setId(String.valueOf(enumConstant.getId()));
        vo.setConstantKey(enumConstant.getConstantKey());
        vo.setConstantName(enumConstant.getConstantName());
        vo.setItemType(enumConstant.getItemType());
        vo.setDescription(enumConstant.getDescription());
        vo.setIsEnable(enumConstant.getIsEnable());
    }

    public static ConstantItem convertConstantItemVoToConstantItem(ConstantItemVo vo) {
        ConstantItem dao = new ConstantItem();
        fillConstantItemFromConstantItemVo(dao, vo);
        return dao;
    }

    public static void fillConstantItemFromConstantItemVo(ConstantItem dao, ConstantItemVo vo) {
        dao.setConstantKey(vo.getConstantKey());
        dao.setItemKey(vo.getItemKey());
        dao.setItemName(vo.getItemName());
        dao.setDescription(vo.getDescription());
    }

    public static ConstantItemVo convertConstantItemToConstantItemVo(ConstantItem constantItem) {
        ConstantItemVo vo = new ConstantItemVo();
        fillConstantItemVoFromConstantItem(vo, constantItem);
        return vo;
    }

    private static void fillConstantItemVoFromConstantItem(ConstantItemVo vo, ConstantItem constantItem) {
        vo.setId(String.valueOf(constantItem.getId()));
        vo.setItemKey(constantItem.getItemKey());
        vo.setItemName(constantItem.getItemName());
        vo.setDescription(constantItem.getDescription());
        vo.setConstantKey(constantItem.getConstantKey());
    }

    public static OptionVo convertEnumConstantToOptionVo(EnumConstant enumConstant) {
        OptionVo vo = new OptionVo();
        vo.setKey(enumConstant.getConstantKey());
        vo.setValue(enumConstant.getConstantName());
        return vo;
    }

    public static OptionVo convertBusinessModelToOptionVo(BusinessModel businessModel) {
        OptionVo vo = new OptionVo();
        vo.setKey(businessModel.getModelKey());
        vo.setValue(businessModel.getModelName());
        return vo;
    }

    public static OptionVo convertRuleScenariosToOptionVo(RuleScenarios ruleScenarios) {
        OptionVo vo = new OptionVo();
        vo.setKey(ruleScenarios.getScenariosKey());
        vo.setValue(ruleScenarios.getScenariosName());
        return vo;
    }

    public static FieldInfoVo convertFieldInfoBoToFieldInfoVo(FieldInfoBo fieldInfoBo) {
        FieldInfoVo vo = new FieldInfoVo();
        vo.setModelKey(fieldInfoBo.getModelKey());
        vo.setFieldName(fieldInfoBo.getFieldName());
        vo.setModelFieldName(fieldInfoBo.getModelFieldName());
        vo.setModelFieldZhName(fieldInfoBo.getModelFieldZhName());
        vo.setFieldValueType(fieldInfoBo.getFieldValueType());
        vo.setFieldType(fieldInfoBo.getFieldType());
        vo.setBasicFieldType(fieldInfoBo.getBasicFieldType());
        return vo;
    }
}
